<?php

namespace app\api\admin\v1\service\finder;

use zhanshop\App;
use zhanshop\server\Request;
use zhanshop\server\Response;
use app\helper\ProxyAccountShareData;

class Proxyaccount extends BaseFinder
{
    public function post(Request $request, Response $response){
        $result = parent::post($request, $response);
        $user = $request->param(0);

        App::callWorker(ProxyAccountShareData::class)->set($user['name'], [
            'name' => $user['name'],
            'pwd' => $user['password'],
        ]);

        return $result;
    }

    public function put(Request $request, Response $response)
    {
        $user = $request->param(0);
        $oldName = App::database()->model($this->menuData['table_names'][0])->where([$this->menuData['pk'] => $request->get('pk')])->value('name');
        if($oldName != $user['name']){
            App::error()->setError('name禁止修改', 403);
        }

        App::callWorker(ProxyAccountShareData::class)->set($user['name'], [
            'name' => $user['name'],
            'pwd' => $user['password'],
        ]);

        return [];
    }

    public function delete(Request $request, Response $response){
        $input = $request->post();
        $accounts = App::database()->model("proxy_account")->whereIn($this->menuData['pk'], $input['pk'])->select();
        foreach($accounts as $account){
            App::database()->model("proxy_account")->where([$this->menuData['pk'] => $account['id']])->delete();

            App::callWorker(ProxyAccountShareData::class)->del($account['name']);

        }

        return [];
    }
}